/*
 * @Author: jianglijun 
 * @Date: 2019-04-20 21:28:52 
 * @Last Modified by: jianglijun
 * @Last Modified time: 2019-04-24 19:40:33
 */
let { findByusername, registerMyuser,findByName,getMusicAll} = require("../models/user.js");
let { isContained , tranJson } = require("../utils/utils.js");
var md5 = require("md5");
module.exports = {

    /**********************  查询是否存在该用户    *******************/
    checkUsername: async (ctx,next) => {
        let param = ctx.request.body;
        if((!param.username)){
            param = tranJson(param);
        }
        let flag = isContained(["username"],Object.keys(param));
        if(!flag){
            ctx.body = {
                erron:1001,
                msg:"字段不正确"
            }
            return;
        }
        try {
            let data = await findByusername(param["username"]);
            if( data.length ===0 ){
                ctx.body = {
                    erron:0,
                    msg:"没有用户名"
                }
                return;
            }
            ctx.body = {
                erron:1,
                msg:"此用户名已经存在"
            }
        } catch (error) {
            ctx.throw(error)
        }
    },

    /**********************  注册用户    *******************/
    registerUser: async (ctx,next) => {
        var md5 = require("md5");
        let param = ctx.request.body;
        if(param.v_code != ctx.session.v_code ){
            ctx.body = {
                erron:1001,
                msg:"验证码不正确"
            }
            return;
        }
        if( (!param.username) && (!param.password) ){
            param = tranJson(param);
        }
        
        if(Object.keys(param).length < 4){
            ctx.body = {
                erron:1001,
                msg:"字段不正确"
            }
            return;
        }
        let flag = isContained(["username","password","telephone","email","sex","grade","v_code"],Object.keys(param),false);
        if(!flag){
            ctx.body = {
                erron:1001,
                msg:"字段不正确"
            }
            return;
        }
        try {
            let data = await findByusername(param["username"]);
            if(data.length>0){
                ctx.body = {
                    erron:1,
                    msg:"此用户名已经存在"
                }
                return;
            }
            let {username,password,telephone,email,sex,grade} = param;
            if( (!username) || (!password) || (!telephone) || (!email)){
                ctx.body = {
                    erron:4,
                    msg:"用户名,密码,手机号,邮箱不能为空"
                }
                return;
            }
            let {affectedRows,insertId} = await registerMyuser(username,md5(password),telephone,email,sex,grade);
            if(affectedRows&&insertId){
                ctx.body = {
                    erron:0,
                    msg:"插入数据成功"
                }
                return;
            }
            ctx.body = {
                erron:2,
                msg:"插入失败"
            }
            return;
        } catch (error) {
            ctx.throw(error)
        }
    },
    
    /**********************  用户登录    *******************/
    userLogin: async (ctx,next) => {
        let param = ctx.request.body;
        if((!param.username) && (!param.password)){
            param = tranJson(param);
        }
        if(param.username=="" && param.password){
            ctx.body = {
                erron:1001,
                msg:"用户名密码不能为空"
            }
            return;
        }
        let {username,password} = param;
        let flag = isContained(["username","password"],Object.keys(param),true);
        if(!flag){
            ctx.body = {
                erron:1001,
                msg:"字段不正确"
            }
            return;
        }
        try {
            let data = await findByusername(username);
            if(data.length){
                // 此用户名存在
                if(data[0].password){
                    if(md5(password) === data[0].password){
                        // 密码正确登录
                        ctx.body = {
                            erron:0,
                            msg:"密码正确,登录",
                            data: data[0].id
                        }
                       ctx.session.user = data[0];
                    }else{
                        // 密码不正确
                        ctx.body = {
                            erron:10,
                            msg:"密码不正确,登录失败"
                        }
                      
                    }
                }
                else{
                    ctx.body = {
                        erron:6,
                        msg:"数据库密码为空"
                    }
                }
            }else{
                // 此用户名不存在
                ctx.body = {
                    erron:1001,
                    msg:"用户名不存在"
                }
            }
        } catch (error) {
            ctx.throw(error)            
        }
    },

    /***************** 注册成功后的登录 ******************* */
    regloign: async (ctx,next) => {
        let data = await findByusername(ctx.request.body["username"]);
        ctx.body = {
            error:0,
            msg:"成功",
            data
        } 
        ctx.session.user = data[0];
    },
    /***************** 验证码 ******************* */
    getPic: async (ctx,next) => {
        const captchapng = require('captchapng2');
        let rand = parseInt(Math.random() * 9000 + 1000);
        let png = new captchapng(80, 30, rand); // width,height, numeric captcha
        ctx.session.v_code = rand + '';
        ctx.body = (png.getBuffer());
    },

     /*************************   获取用户所有的音乐 ************************* */
     async getuserMusicAll(ctx,next) {
       let uid = ctx.session.user.id;
         try {
             let data = await getMusicAll(+uid);
             ctx.body = {
                 error:0,
                 msg:"成功",
                 data
             }      
         } catch (error) {
             ctx.throw(error);
         }
     },

     /****************************  用户是否登录  ************************* */
     async isLogin(ctx,next) {
         if(ctx.session.user){
            ctx.body = {
                erron:0,
                msg:"此用户已经登录了",
                data:{
                    username:ctx.session.user.username,
                    uid:ctx.session.user.id
                }
            }
         }else{
             ctx.body = {
                 erron:10,
                 msg:"未登录"
             }
         }
         
     },

     async outlogin(ctx,next) {
         ctx.session.user=null;
         ctx.redirect("/user/login")
     }
}